package com.shenhaofeng.app.http.url;

import android.text.TextUtils;

import com.shenhaofeng.app.http.Params;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/10/12.
 */
public class HttpParamsUrl extends BaseHttpUrl {


    private Params params;


    private HttpParamsUrl(String url, Params params) {
        super(url);
        if (params.size() > 0) {
            String newUrl = url + "?" + urlParams(params);
            setUrl(newUrl);
        }
    }


    private HttpParamsUrl(String url) {
        this(url, new Params.Builder().build());
    }


    public static HttpParamsUrl create(String url) {
        return new HttpParamsUrl(url);
    }


    public static HttpParamsUrl create(String url, Params params) {
        return new HttpParamsUrl(url, params);
    }


    public Params getParams() {
        return params;
    }


    private String urlParams(Params params) {
        List<String> kvs = new ArrayList<>();
        for (int i = 0; i < params.size(); i++) {
            kvs.add(String.format("%s=%s", params.name(i), params.value(i)));
        }
        return TextUtils.join("&", kvs);
    }


}
